<!DOCTYPE html>
<html lang="en">

<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <meta http-equiv="X-UA-Compatible" content="ie=edge">
    <title>Document</title>
    <style>
        * {
            padding: 0;
            margin: 0;
        }

        ul {
            list-style: none;
        }

        .tabBox {
            box-sizing: border-box;
            margin: 20px auto;
            width: 500px;
        }

        #navBox {
            display: flex;
        }

        li {
            margin-right: 10px;
            line-height: 35px;
            padding: 0 10px;
            border: 1px solid red;
            box-sizing: border-box;
            cursor: pointer;
        }

        .tabBox>div {
            display: none;
            box-sizing: border-box;
            padding: 10px;
            height: 150px;
            border: 1px solid #999;
            margin-top: -1px;
            z-index: -2;
        }

        .active {
            border-bottom-color: #fff;
            background-color: #fff;
        }

        .tabBox>div.active {
            display: block;
        }
    </style>
</head>

<body>
    <div class="tabBox">
        <ul id="navBox">
            <li class="active">编程</li>
            <li>读书</li>
            <li>运动</li>
        </ul>
        <div class="active">编程快乐</div>
        <div>读书幸福</div>
        <div>运动健康</div>
    </div>
    <script>


        var tabBox = document.querySelector('.tabBox')
        var tabList = document.querySelectorAll('.tabBox > div')
        var navBox = document.querySelector('#navBox')
        var navList = document.querySelectorAll('#navBox > li')

        /*     for (var j = 0; j < navList.length; j++) {
                navList[j].myindex = j
                navList[j].onclick = function () {
                    changeTab(this.myindex)
                }
            } */

        for (let j = 0; j < navList.length; j++) {
            navList[j].onclick = function () {
                changeTab(j)
            }
        }
        
        function changeTab(clickIndex) {
            for (let i = 0; i < navList.length; i++) {
                navList[i].className = '';
                tabList[i].className = '';
            }
            navList[clickIndex].className = 'active';
            tabList[clickIndex].className = 'active';
        }

    </script>

</body>

</html>